package com.ruirenyun.config.filter;

/**
 * Created by xiaobo on 2014/9/1.
 */

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RequestFilter implements Filter {

    /**
     * 创建线程
     */
    public static final ThreadLocal<HttpServletRequest> THREAD_LOCAL = new ThreadLocal<>();

    @Override
    public void destroy() {
        THREAD_LOCAL.remove();
    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
                         FilterChain arg2) throws IOException, ServletException {

        THREAD_LOCAL.set((HttpServletRequest) arg0);
        HttpServletResponse response = ((HttpServletResponse) arg1);
        response.addHeader("Cache-Control", "no-cache");
        response.addHeader("Pragma", "No-cache");
        response.addDateHeader("Expires", 0);
        arg2.doFilter(arg0, arg1);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // default implementation ignored
    }

}

